﻿import wepy from 'wepy'
import {ACCESS_TOKEN} from './constant'
import tip from '@/utils/tip';
//const apiLink = 'https://apicms.yla520.com'
const apiLink = "https://apidevelop.yla520.com"

const requestFun = (params,URL) => {
    let data = params.query || {};
    let promise = new Promise(function (resolve,reject) {
          wepy.request({ 
          url: apiLink+URL, //开发者服务器接口地址",
          data: data, //请求的参数",
          method:params.method || 'POST',
          header: { 
              'Content-Type': 'application/json',
               'Authorization':'Bearer ' + wepy.getStorageSync(ACCESS_TOKEN),
        },
          success: res => {
              if(res.statusCode === 200)
              {
                resolve(res.data)
              }
              else
              { 
                  reject(res)
              }
          }, 
          fail: (err) => {
              reject(err)
          },
          complete:() => {
            tip.hideLoading();
          }
        });
    })
    return promise;
}

//图片上传（请参数小程序文件上传）  
const uploadFile = (params,URL) => {
    console.log(params)
    let promise = new Promise((resolve,reject) => {
        wepy.uploadFile({
            url: apiLink + URL, 
            filePath: params.filepath, //要上传文件资源的路径
            name: params.name, //文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容,
            formData: {
                memberId:params.memberid,
            },
            success: res => {
                resolve(res)
            },
            fail: (err) => {
                reject(err);
            }
          });
    })
    return promise;
}

//咨询请求end
module.exports = {
    requestFun,
    uploadFile
}